<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="handleclose" @open="handleOpen">
    <!-- 表单 -->
    <el-form ref="employeeFormRef" label-width="120px" :model="employeeForm" :rules="employeeFormRules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employeeForm.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="employeeForm.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="employeeForm.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employeeForm.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in hireTypes"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employeeForm.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="employeeForm.departmentName" prop="departmentName" style="width:80%" placeholder="请选择部门" readonly @focus="isShow=true" />
        <!-- 部门树形组件 -->
        <el-tree v-if="isShow" :data="deptTreeData" :props="{label:'name'}" @node-click="handleNodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="employeeForm.correctionTime" style="width:80%" placeholder="请选择转正时间" value-format="yyyy-MM-dd" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleclose">取消</el-button>
      <el-button type="primary" @click="handleAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import obj from '@/constant/employees'
import { getDeptListAPI } from '@/api/departments'
import { addEmployeeAPI } from '@/api/employees'
import { list2Tree } from '@/utils/switch'
export default {
  name: 'Addemployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 聘用形式列表
      hireTypes: obj.hireType,
      //   部门树形结构数据
      deptTreeData: [],
      //   控制树形组件的显示隐藏
      isShow: false,
      //  员工表单
      employeeForm: {
        username: '',
        mobile: '',
        formOfEmployment: 1,
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      employeeFormRules: {}
    }
  },
  methods: {
    // 弹框关闭:子传父
    handleclose() {
      this.$emit('update:showDialog', false)
      //   弹窗关闭时，重置表单
      this.$refs.employeeFormRef.resetFields()
    },
    // 弹框打开时，发请求获取部门列表
    async handleOpen() {
      const resp = await getDeptListAPI()
      //   转为树形结构再赋值
      this.deptTreeData = list2Tree(resp.data.depts, '')
    },
    // 树形节点点击时
    handleNodeClick(node) {
      // node:被点击的部门对象
      if (node.children) return
      this.employeeForm.departmentName = node.name
      this.isShow = false
    },
    // 新增员工
    async handleAdd() {
      await addEmployeeAPI(this.employeeForm)
      this.handleclose()
      this.$emit('add-success')
    }
  }

}
</script>

  <style lang="scss" scoped >
.el-tree {
  position: absolute;
  width: 80%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
  </style>
